import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from "../views/Login"
import Order from "../views/Order";
import Class from "../views/Class";
import User from "../views/User"
import Article from "../views/Article";
import AddArticle from "../views/atricle/AddArticle"
import ListArticle from "../views/atricle/ListArticle"
import AddClass from "../views/class/AddClass";
import ListClass from "../views/class/ListClass"
import store from "../store"

Vue.use(VueRouter)

export const menu_list = [
    {
      path: '/',
      name: 'Home',
      component: Home,
      icon:"el-icon-school",
      title:"信息展示",
        meta:{
          title:"信息展示"
        }
    },
    {
      path:"/order",
      component:Order,
      icon:"el-icon-shopping-cart-1",
      title:"订单管理",
      meta:{
        title:"订单管理"
      }
    },
    {
      path:"/user",
      component:User,
      title:"用户管理",
      icon:"el-icon-user",
      meta:{
        title:"用户管理"
      }
    },
    {
      path:"/article",
      component:Article,
      icon:"el-icon-notebook-2",
      title:"文章管理",
      meta:{
        title:"文章管理"
      },
      children:[
        {
          path:"/article",
          component: AddArticle,
          title:'添加文章',
          meta:{
            title:"添加文章"
          }
        },
        {
          path:"/article/list",
          component: ListArticle,
          title:'文章列表',
          meta:{
            title:"文章列表"
          }
        }
      ]
    },
    {
      path:"/class",
      component:Class,
      icon:"el-icon-video-camera",
      title:"课程管理",
      meta:{
        title:"课程管理"
      },
      children:[
        {
          path:"/class",
          component: AddClass,
          title:'添加课程',
          meta:{
            title:"添加课程"
          }
        },
        {
          path:"/class/list",
          component: ListClass,
          title:'课程列表',
          meta:{
            title:"课程列表"
          }
        }
      ]
    },
  
]


const routes = [
 
  {
    path: '/login',
    name: 'Login',
    component:Login,
    meta:{
      title:"请登录"
    }
  },
  ...menu_list
  
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


//登陆验证
router.beforeEach((to,from,next)=>{
   document.title = to.meta.title;
   
  // 通过user_id
  let user_id = store.state.user.user_id;
  console.log(user_id)
  // 
  if(user_id){
    if(to.path==="/login"){
      next("/")
    }else{
      next() 
    }
  }else{
    //未登录 
    if(to.path=="/login"){
      next()
    }else{
      next("/login")
    }
    
  }

})

export default router
